package com.ushine.design.jdk8.optional;

import java.util.Optional;

/**
 * @Author: yls
 * @Date: 2021/8/30 14:23
 * @Description: 【jdk1.8】对空的判断和处理
 * @Version 1.0
 */
public class OptionClass {
    public static void main(String[] args) {
        User user1 = null;
        User user2 = new User("zhangsan", "123456");

        /**
         * 判空并提供默认值
         */
        User resultUser = Optional.ofNullable(user1).orElse(user2);
        System.err.println(resultUser);
        String str = null;
        String tempStr = Optional.ofNullable(str).orElse("");
        System.err.println("".equals(tempStr));
        /**
         * 测试【get】和【isPresent】
         */
        User user = new User("zhangsan", "123456");
        Optional<User> optionalUser = Optional.ofNullable(user);
        System.err.println("get()：" + optionalUser.get());
        System.err.println("isPresent()：" + optionalUser.isPresent());
        /**
         * 测试【ifPresent】
         */
        optionalUser.ifPresent(user3 -> System.err.println("ifPresent(对象非空)：" + user3));
        user = null;
        optionalUser = Optional.ofNullable(user);
        optionalUser.ifPresent(user3 -> System.err.println("ifPresent(对象为空)：" + user3));

        /**
         * 测试【orElse】和【orElseGet】
         */
        User tempUser = null;
        User user3 = Optional.ofNullable(tempUser).orElse(createUser());
        System.err.println("tempUser为空，user3：" + user3);
        User user4 = Optional.ofNullable(tempUser).orElseGet(() -> createUser());
        System.err.println("tempUser为空，user4：" + user4);

        tempUser = createUser();
        System.err.println("tempUser不为空，orElse执行分析：");
        user3 = Optional.ofNullable(tempUser).orElse(createUser());
        System.err.println("tempUser不为空，user3：" + user3);
        user4 = Optional.ofNullable(tempUser).orElseGet(() -> createUser());
        System.err.println("tempUser不为空，user4：" + user4);

        /**
         * 测试抛出异常
         */
        try {
            orElseThrow();
        } catch (Exception e) {
            System.err.println("抛出异常：" + e.getClass() + "《=》" + e.getMessage());
        }

        /**
         * 转换值
         */
        User user5 = new User("wangwu", "8888888");
        String username = Optional.ofNullable(user5).map(u -> u.getUsername()).orElse("zhangsan");
        System.err.println(username);

        user5.setPosition("四川省");
        String position = Optional.ofNullable(user5).flatMap(u -> u.getPosition()).orElse("");
        System.err.println("flatMap转换值：" + position.equals(user5.getPosition().get()));

        /**
         * 过滤值
         */
        Optional<User> filterUser = Optional.ofNullable(user5).filter(u -> u.getUsername() != null && u.getUsername().contains("wang"));
        System.err.println(filterUser);

        /**
         * 链式结构
         */
        User user6 = new User("zhangsan", "23232");
        String result = Optional.ofNullable(user6)
                .flatMap(User::getAddress)
                .flatMap(Address::getCountry)
                .map(Country::getAreaCode).orElse("1231");
        System.err.println(result);

        /**
         * 【ifPresent】：如果创建的Optional中的值存在，则执行该方法的调用，否则什么也不做
         */
        Optional<User> user61 = Optional.ofNullable(user6);
        user61.ifPresent(e -> System.err.println("被处理:" + e));
        user61 = Optional.ofNullable(null);
        user61.ifPresent(e -> System.err.println(e));

    }


    public static User createUser() {
        System.err.println("调用createUser");
        return new User("lisi", "666666");
    }

    public static User orElseThrow() {
        User user = null;
        Optional.ofNullable(user).orElseThrow(() -> new NullPointerException("用户为空"));
        return user;
    }
}
